package vcp.server.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import vcp.entity.Complaint;
import vcp.entity.ComplaintHistory;
import vcp.enums.ComplaintType.ComplaintResolve;
import vcp.enums.SiteEnum;
import vcp.server.DBAdapter;
import vcp.server.DAO.interfaces.IGenericDAO;
import vcp.server.utility.Constants.ComplaintHistoryTable;
import vcp.server.utility.Constants.TableNames;
import vcp.server.utility.SQLQueries;
/**
 * <p> complaint history DAO class , implements relevant sql queries </p> 
 * @author VCP team
 *
 */
public class ComplaintHistoryDAO implements IGenericDAO <ComplaintHistory, Complaint>
{
	// ****************************************************************************************************************************
	// Public method
	// ****************************************************************************************************************************
	/**
	 * finds complaint by id per given date
	 * @param complaintID
	 * @param endDate
	 * @param site
	 * @return complaint history
	 */
	public ComplaintHistory findComplaintByIDTillDate(int complaintID,DateTime endDate,SiteEnum site)
	{
		ComplaintHistory complaintHistory = null;

		String query =
				SQLQueries.SELECT_ALL
				+ SQLQueries.FROM + TableNames.COMPLAINT_HISTORY
				+ " " + SQLQueries.WHERE
				+ " " + ComplaintHistoryTable.SITE_NAME + "=" + "\"" + site.toString()+"\""
				+ " " + SQLQueries.AND
				+ " " + ComplaintHistoryTable.COMPLAINT_ID + "=" + complaintID
				+ " " + SQLQueries.AND
				+ SQLQueries.DATE + "(" + ComplaintHistoryTable.CLOSE_DATE + ")"
				+ SQLQueries.LTE
				+ "\"" + DateTimeFormat.forPattern("yyyy-MM-dd").print(endDate) + "\"";

		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if ( set.next() )
			{
				complaintHistory = this.create();

				complaintHistory.setID(set.getInt(ComplaintHistoryTable.ID));
				complaintHistory.setComplaintID(set.getInt(ComplaintHistoryTable.COMPLAINT_ID));
				complaintHistory.setComplaintResolveText(set.getString(ComplaintHistoryTable.RESOLVE_TEXT));
				complaintHistory.setComplaintResolveStatus(ComplaintResolve.convert(set.getString(ComplaintHistoryTable.RESOLVE_STATUS)));
				complaintHistory.setRefundAmount(set.getInt(ComplaintHistoryTable.REFUND_AMOUNT));
				complaintHistory.setSiteEnum(SiteEnum.convert(set.getString(ComplaintHistoryTable.SITE_NAME)));
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		return complaintHistory;
	}


	// ****************************************************************************************************************************
	// IGenericDAO method
	// ****************************************************************************************************************************
	@Override
	public Integer findMaxID ()
	{
		Integer complaintHistoryID = null;
		String query =
				String.format(
						SQLQueries.SELECT_MAX__VALUE__AS__NAME__FROM__TABLE_NAME__
						, ComplaintHistoryTable.ID
						, SQLQueries.MAX_ID
						, TableNames.COMPLAINT_HISTORY);

		try
		{
			ResultSet set = DBAdapter.getAdapter().find(query);
			if ( set.next() )
			{
				complaintHistoryID = set.getInt("maxID");
			}
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		if ( complaintHistoryID == null )
		{
			complaintHistoryID = 0;
		}

		return ++ complaintHistoryID;
	}

	@Override
	public ComplaintHistory create ()
	{
		return new ComplaintHistory();
	}

	@Override
	public ComplaintHistory findByEntity ( Complaint entity )
	{
		return null;
	}

	@Override
	public ComplaintHistory findByID ( int ID )
	{
		return null;
	}

	@Override
	public ComplaintHistory findByID ( String ID )
	{
		return null;
	}

	@Override
	public List <ComplaintHistory> findAll ( Complaint info )
	{
		return null;
	}

	@Override
	public boolean save ( ComplaintHistory entity )
	{
		boolean result = false;
		String valuesSet =
				String.format(
						"%d,%d,\"%s\",\"%s\",%d,\"%s\",\"%s\""
						, entity.getID()
						, entity.getComplaintID()
						, entity.getComplaintResolveText()
						, entity.getComplaintResolveStatus().toString()
						, entity.getRefundAmount()
						, new Timestamp(entity.getCloseDate().getMillis())
						, entity.getSiteEnum().toString());

		String sqlQuery =
				String.format(
						SQLQueries.INSERT_INTO__TABLENAME__VALUES__
						, TableNames.COMPLAINT_HISTORY
						, valuesSet);

		try
		{
			result = DBAdapter.getAdapter().save(sqlQuery);
		}
		catch ( SQLException e )
		{
			e.printStackTrace();
		}

		return result;
	}

	@Override
	public boolean refresh ( String sqlQuery )
	{
		return false;
	}

	@Override
	public boolean deleteByID ( int ID )
	{
		return false;
	}

	@Override
	public boolean deleteByID ( String ID )
	{
		return false;
	}

}
